project(lua)

if((CMAKE_SYSTEM_NAME MATCHES "Linux") OR (CMAKE_SYSTEM_NAME MATCHES "Android"))
    add_definitions(-DLUA_USE_LINUX)
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
    add_definitions(-DLUA_USE_MACOSX)
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
    add_definitions("/wd5105")
endif()

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} LIB_LUA_SOURCES)
list(REMOVE_ITEM LIB_LUA_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/lua.c ${CMAKE_CURRENT_SOURCE_DIR}/luac.c)
if((CMAKE_SYSTEM_NAME MATCHES "Linux") OR (CMAKE_SYSTEM_NAME MATCHES "Android"))
    add_library(liblua SHARED ${LIB_LUA_SOURCES})   
    target_link_libraries(liblua dl readline m mimalloc)
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
    add_library(liblua SHARED ${LIB_LUA_SOURCES})
    target_link_libraries(liblua readline m mimalloc)
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
    add_library(liblua ${LIB_LUA_SOURCES})
    target_link_libraries(liblua mimalloc)
endif()
set_target_properties(liblua PROPERTIES OUTPUT_NAME "lua")

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} LUA_SOURCES)
list(REMOVE_ITEM LUA_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/luac.c)
add_executable(lua ${LUA_SOURCES})
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
    set_target_properties(lua PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${EXE})
    set_target_properties(lua PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${EXE})
    set_target_properties(lua PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${EXE})
    set_target_properties(lua PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${EXE})
endif()
if((CMAKE_SYSTEM_NAME MATCHES "Linux") OR (CMAKE_SYSTEM_NAME MATCHES "Android"))
    target_link_libraries(lua dl readline m mimalloc)
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
    target_link_libraries(lua readline m mimalloc)
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
    target_link_libraries(lua mimalloc)
endif()

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} LUAC_SOURCES)
list(REMOVE_ITEM LUAC_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/lua.c)
add_executable(luac ${LUAC_SOURCES})
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
    set_target_properties(luac PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${EXE})
    set_target_properties(luac PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${EXE})
    set_target_properties(luac PROPERTIES RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${EXE})
    set_target_properties(luac PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${EXE})
endif()
if((CMAKE_SYSTEM_NAME MATCHES "Linux") OR (CMAKE_SYSTEM_NAME MATCHES "Android"))
    target_link_libraries(luac dl readline m mimalloc)
elseif(CMAKE_SYSTEM_NAME MATCHES "Darwin")
    target_link_libraries(luac readline m mimalloc)
elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
    target_link_libraries(luac mimalloc)
endif()
